﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using buildingLib.building;
using buildingLib.fee;
using DocPrinter;
using buildingLib.security;

namespace _dmis.fee
{
    public partial class FeeNotifyForm : _dmis.UIFormBase
    {
        #region ICommand
        public static string MenuItemName()
        {
            return "收费通知单";
        }
        public static Image MenuItemImage()
        {
            return _dmis.Properties.Resources.mail_notice.ToBitmap();
        }
        public static void MF_DoExcecute()
        {
            FeeNotifyForm dlg = new FeeNotifyForm();
            dlg.ShowDialog();
        }

        public static void MF_DoUpdateUI(ToolStripItem tsItem)
        {
            tsItem.Enabled = ACLTable.CanEdit(FeeNotify.GetACLID());
        }
        #endregion

        FeeNotify feeNotify;
        int printCount = 0;
        FeeTemplateList feeTemplates;
        public FeeNotifyForm()
        {
            InitializeComponent();
        }
        private void LoadDoors()
        {
            this.tv_Door.Nodes.Clear();
            //取出所有的根结点及所有的子节点所在的DoorItem对象
            buildingLib.building.DoorItem items = DataCaches.CacheDoors();

            foreach (DoorItem item in items.Items)
            {
                //读取根节点
                TreeNode node = new TreeNode(item.Name);
                node.Tag = item.ID;
                FillChild(node, item);
                tv_Door.Nodes.Add(node);
            }
        }
        private void FillChild(TreeNode node, DoorItem item)
        {
            foreach (DoorItem child in item.Items)
            {
                //读取根下面的子节点再遍历出来
                TreeNode childNode = new TreeNode(child.Name);
                childNode.Tag = child.ID;
                FillChild(childNode, child);
                node.Nodes.Add(childNode);
            }
        }

        private void FeeNotify_Load(object sender, EventArgs e)
        {
            feeTemplates = FeeTemplateList.GetLists("type = 1 and ");
            feeTemplateListBindingSource.DataSource = feeTemplates;
            feeNotify = FeeNotify.NewFeeNotify();
            invoiceItemsBindingSource.DataSource = InvoiceItems.NewInvoiceItems();
            LoadDoors();
        }

        private void tv_Door_AfterCheck(object sender, TreeViewEventArgs e)
        {
            
            if (e.Node.Checked)
            {
                if (e.Node.Parent != null)
                {
                    if (e.Node.Parent.Parent != null)
                    {
                        if (e.Node.Parent.Parent.Parent != null)
                        {
                            e.Node.Parent.Parent.Parent.Checked = true;
                        }
                        e.Node.Parent.Parent.Checked = true;
                    }
                    e.Node.Parent.Checked = true;
                }
            }
            else
            {
                bool found1 = false;
                bool found2 = false;
                bool found3 = false;
                if (e.Node.Parent != null)
                {
                    if (e.Node.Parent.Parent != null)
                    {
                        if (e.Node.Parent.Parent.Parent != null)
                        {
                            foreach (TreeNode tn in e.Node.Parent.Parent.Parent.Nodes)
                            {
                                if (tn.Checked == true)
                                {
                                    found1 = true;
                                    break;
                                }
                            }
                            if (found1 == false)
                                e.Node.Parent.Parent.Parent.Checked = false;
                        }
                        foreach (TreeNode tn in e.Node.Parent.Parent.Nodes)
                        {
                            if (tn.Checked == true)
                            {
                                found2 = true;
                                break;
                            }
                        }
                        if (found2 == false)
                            e.Node.Parent.Parent.Checked = false;
                    }
                    foreach (TreeNode tn in e.Node.Parent.Nodes)
                    {
                        if (tn.Checked == true)
                        {
                            found3 = true;
                            break;
                        }
                    }
                    if (found3 == false)
                        e.Node.Parent.Checked = false;
                }
            }
            Guid doorID = Guid.Empty;
            foreach (TreeNode node in tv_Door.Nodes)
            {
                doorID = GetFirstDoorID(node);
                if (doorID != Guid.Empty)
                {
                    foreach(InvoiceItem item in ((InvoiceItems)invoiceItemsBindingSource.DataSource))
                    {
                        item.DoorID = doorID;
                    }
                    break;
                }
            }
            invoiceItemsBindingSource.ResetBindings(false);
        }

        private void tv_Door_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
            {
                SelectAllNodes(e.Node);
            }
        }

        //选中所有子节点
        private void SelectAllNodes(TreeNode node)
        {
            if (node.Nodes == null || node.Nodes.Count <= 0)
                return;
            foreach (TreeNode ChildNode in node.Nodes)
            {
                ChildNode.Checked = node.Checked;
                SelectAllNodes(ChildNode);
            }
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FeeTypeSelector dlg = new FeeTypeSelector((InvoiceItems)invoiceItemsBindingSource.DataSource);
            dlg.ShowDialog(this);
            Guid doorID = Guid.Empty;
            
            foreach(TreeNode node in tv_Door.Nodes)
            {
                doorID = GetFirstDoorID(node);
                if (doorID != Guid.Empty)
                {
                    ((InvoiceItems)invoiceItemsBindingSource.DataSource)[((InvoiceItems)invoiceItemsBindingSource.DataSource).Count - 1].DoorID = doorID;
                    break;
                }
            }
                
            dgvItems[0, dgvItems.Rows.Count - 1].Value = dlg.SelectedName;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            ((InvoiceItems)invoiceItemsBindingSource.DataSource).Remove((InvoiceItem)invoiceItemsBindingSource.Current);
        }

        private void dgvItems_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 0)
                return;
            FeeTypeSelector dlg = new FeeTypeSelector();
            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                ((InvoiceItem)invoiceItemsBindingSource.Current).Name = dlg.SelectedName;
                ((InvoiceItem)invoiceItemsBindingSource.Current).TypeID = dlg.SelectedID;
                dgvItems[e.ColumnIndex,e.RowIndex].Value = dlg.SelectedName;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //判断输入
            if (txtName.Text == "")
            {
                lblError.Visible = true;
                lblError.Text = "错误：名称为必填项！";
                return;
            }
            //循环所有房间，插入数据
            foreach (TreeNode node in tv_Door.Nodes)
            {
                ForAllNodes(node);
            }
            //添加信息到FeeNotify
            feeNotify.Name = txtName.Text;
            feeNotify.TempleteItems = (InvoiceItems)invoiceItemsBindingSource.DataSource;
            feeNotify.Save();

            //询问是否打印单据
            if (MessageBox.Show("保存成功，是否打印所有通知单？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {

                pdlgTicket.Document = pdocTicket;
                
                if (pdlgTicket.ShowDialog(this) == DialogResult.OK)
                {
                    pdlgTicket.AllowSomePages = true;
                    pdocTicket.Print();
                }

            }

            //成功后推出该窗口
            this.Close();
        }

        //遍历所有节点，添加选中节点
        private void ForAllNodes(TreeNode node)
        {
            if (node.Level < 2)
            {
                foreach (TreeNode item in node.Nodes)
                {
                    if (node.Checked == false)
                        continue;
                    ForAllNodes(item);
                }
                return;
            }
            if (node.Checked == true)
            {
                FeeNotifyItem feeItem = FeeNotifyItem.NewFeeNotifyItem((Guid)node.Tag, feeNotify.Id);
                feeItem.Name = txtName.Text;
                feeItem.Items = ((InvoiceItems)invoiceItemsBindingSource.DataSource).Clone();
                foreach (InvoiceItem item in feeItem.Items)
                {
                    item.DoorID = feeItem.DoorID;
                }
                feeNotify.Items.Add(feeItem);
            }
        }
        private Guid GetFirstDoorID(TreeNode node)
        {
            Guid doorID = Guid.Empty;
            foreach (TreeNode item in node.Nodes)
            {
                if(item.Level == 2 && item.Checked)
                {
                    return new Guid(item.Tag.ToString());
                }
                if (item.Level != 2)
                {
                    doorID = GetFirstDoorID(item);
                    if (doorID != Guid.Empty)
                        return doorID;
                }
            }
            return Guid.Empty;
            
        }

        private void comboBoxFeeTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxFeeTemplate.SelectedValue == null)
                return;
            ((InvoiceItems)invoiceItemsBindingSource.DataSource).Clear();
            FeeTemplate m_template = FeeTemplate.GetFeeTemplate((Guid)comboBoxFeeTemplate.SelectedValue);
            foreach(InvoiceItem item in m_template.Items)
            {
                ((InvoiceItems)invoiceItemsBindingSource.DataSource).Add(item);
                //dgvItems[0, dgvItems.Rows.Count - 1].Value = item.Name;
            }
        }

        private void pdocTicket_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //打印单据
            FeeNotifyItem item = feeNotify.Items[printCount];
            FeePrinter.PrintFeeNotify(e.Graphics, item, feeNotify.TempleteItems);
            item.UpdateTaskStatus(1);
            e.HasMorePages = false;
            printCount++;
            if (printCount >= feeNotify.Items.Count)
            {
                e.HasMorePages = false;
                return;
            }
            e.HasMorePages = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}